import pytest

from page.page_in import PageIn
from tools.get_driver import GetDriver
import page
from time import sleep

from tools.get_log import GetLog
from tools.read_yaml import read_yaml

logger = GetLog.get_logger()


class TestYxLogin:

    # 初始化
    def setup_class(self):
        # 1.获取driver
        driver = GetDriver.get_web_driver(page.qq_mail_url)
        # sleep(1)
        # driver.switch_to.frame("login_frame")
        self.qq_mail = PageIn(driver).page_get_PageSyLogin()
        # 2.通过统一入口类获取PageSyLogin对象

    # 结束
    def teardown_class(self):
        GetDriver.quit_web_driver()

    # 测试业务方法
    @pytest.mark.parametrize('username, password, expect', read_yaml('qq_mail_login.yaml'))
    def test_yx_login(self, username, password, expect):
        # 1.调用登录业务方法
        self.qq_mail.page_sy_login(username, password)
        sleep(1)
        # 2.断言
        # print('\n 获取的昵称为：', self.qq_mail.page_get_nickname())
        try:
            assert expect == self.qq_mail.page_get_nickname()
        except Exception as e:
            logger.error('断言出错，错误信息:{}'.format(e))
            # 输出错误信息
            print('错误原因 ', e)
            # 截图
            self.qq_mail.base_get_image()
            # 抛异常
            raise
